<template>
  <el-scrollbar :wrap-class="{ wrapClass: true }">
    <div class="app-container">
      <div class="filter-container" size="small">
        <el-input
          v-model="listQuery.name"
          size="small"
          :placeholder="$t('user.userGroupName')"
          style="width: 200px"
          class="filter-item"
          clearable
        />
        <el-select
          v-model="listQuery.state"
          size="small"
          :placeholder="$t('common.status')"
          filterable
          clearable
          class="filter-item"
          style="width: 170px"
        >
          <el-option key="1" :label="$t('common.enable')" value="1" />
          <el-option key="2" :label="$t('common.disabled')" value="2" />
        </el-select>
        <el-button
          class="filter-item"
          size="small"
          icon="el-icon-search"
          type="primary"
          plain
          @click="search"
        >
          {{ $t('table.search') }}
        </el-button>
        <el-button
          class="filter-item"
          size="small"
          icon="el-icon-plus"
          type="primary"
          @click="add"
        >
          {{ $t('table.add') }}
        </el-button>
        <el-button
          size="small"
          class="filter-item right"
          icon="el-icon-refresh"
          type="primary"
          plain
          @click="refresh"
        >
          {{ $t('common.refresh') }}
        </el-button>
      </div>

      <el-table
        ref="multipleTable"
        v-loading="listLoading"
        :data="list"
        :height="
          listQuery.total > 0 ? 'calc(100vh - 205px)' : 'calc(100vh - 170px)'
        "
        :header-cell-style="{ background: '#F2F6FC' }"
        element-loading-text="Loading"
        fit
        border
        size="medium"
      >
        <el-table-column
          align="center"
          :label="$t('common.id')"
          width="50"
          type="index"
        />
        <el-table-column :label="$t('user.ruleTemplateName')" min-width="200px">
          <template slot-scope="scope">
            <strong>{{ scope.row.name }}</strong>
          </template>
        </el-table-column>
        <el-table-column
          :label="$t('common.remark')"
          min-width="200px"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            <strong>{{ scope.row.remark }}</strong>
          </template>
        </el-table-column>
        <el-table-column :label="$t('common.status')" width="80" align="center">
          <template slot-scope="scope">
            <strong v-if="scope.row.state === 1" style="color: #67c23a">{{
              $t('common.enable')
            }}</strong>
            <strong v-else style="color: #f56c6c">{{
              $t('common.disabled')
            }}</strong>
          </template>
        </el-table-column>
        <!--        <el-table-column label="排序" width="60px" align="center">-->
        <!--          <template slot-scope="scope">-->
        <!--            <div class="sort-div">-->
        <!--              <el-tooltip class="item" effect="dark" content="置顶于当前状态" placement="left">-->
        <!--                <el-button type="text" @click="setSort(scope.row.id,1)">-->
        <!--                  <i class="el-icon-arrow-up"/>-->
        <!--                </el-button>-->
        <!--              </el-tooltip>-->
        <!--              <el-tooltip class="item" effect="dark" content="置底于当前状态" placement="left">-->
        <!--                <el-button type="text" @click="setSort(scope.row.id,2)">-->
        <!--                  <i class="el-icon-arrow-down"/>-->
        <!--                </el-button>-->
        <!--              </el-tooltip>-->
        <!--            </div>-->
        <!--          </template>-->
        <!--        </el-table-column>-->
        <el-table-column
          :label="$t('common.createTime')"
          width="160px"
          align="center"
        >
          <template slot-scope="scope">
            {{ scope.row.updatedAt }}
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          fixed="right"
          prop
          :label="$t('common.actions')"
          width="270"
        >
          <template slot-scope="scope">
            <el-button-group>
              <el-button
                type="warning"
                size="mini"
                icon="el-icon-view"
                @click="setAuthority(scope.row)"
                >{{ $t('user.setRule') }}
              </el-button>

              <el-button
                type="primary"
                size="mini"
                icon="el-icon-edit"
                @click="edit(scope.row.id)"
                >{{ $t('common.edit') }}
              </el-button>

              <el-button
                :disabled="scope.row.id == 1"
                type="danger"
                size="mini"
                icon="el-icon-delete"
                @click="deleteRow(scope.row.id)"
                >{{ $t('common.delete') }}
              </el-button>
            </el-button-group>
          </template>
        </el-table-column>
      </el-table>
      <!--分页-->
      <pagination
        v-show="listQuery.total > 0"
        :total="listQuery.total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.limit"
        @pagination="fetchData"
      />

      <edit-page :id="id" :show.sync="showState" @closeAction="closeAction" />
      <authority-page
        :id="id"
        :title="title"
        :show.sync="authorityState"
        @closeAction="closeAuthorityAction"
      />
    </div>
  </el-scrollbar>
</template>
<script>
import Pagination from '@/components/Pagination'
import EditPage from '@/views/userManage/ruleTemplate/edit'
import AuthorityPage from '@/views/userManage/ruleTemplate/authority'
import { getAction, postAction } from '@/api/common'
import common from '@/utils/common'

export default {
  name: 'index',
  components: {
    Pagination,
    EditPage,
    AuthorityPage,
  },
  mounted() {
    this.fetchData()
  },
  data() {
    return {
      listLoading: false,
      list: [],
      listQuery: {
        page: 1,
        total: 0,
        limit: 20,
        name: '',
        state: '',
      },
      id: '',
      title: '',
      showState: false,
      authorityState: false,
    }
  },
  methods: {
    async fetchData() {
      this.listLoading = true
      const { data } = await getAction(this.listQuery, 'ruleTemplate/GetList')
      this.list = data.items
      this.listQuery.total = data.total
      this.listLoading = false
    },
    add() {
      this.id = ''
      this.showState = true
    },
    edit(id) {
      this.id = id
      this.showState = true
    },
    setAuthority(row) {
      this.id = row.id
      this.title = common.subString(row.name, 10)
      this.authorityState = true
    },
    async deleteRow(id) {
      this.$confirm(this.$t('common.confirmDelete'), this.$t('common.tips'), {
        confirmButtonText: this.$t('common.confirm'),
        cancelButtonText: this.$t('common.cancel'),
        type: 'warning',
      })
        .then(async () => {
          await postAction({ id: id }, 'ruleTemplate/delInfo')
          this.$notify({
            title: this.$t('common.success'),
            message: this.$t('common.operationSuccess'),
            type: 'success',
            duration: 2000,
          })
          this.refresh()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: this.$t('common.cancelOperation'),
          })
        })
    },
    search() {
      this.listQuery.page = 1
      this.fetchData()
    },
    refresh() {
      this.fetchData()
    },
    closeAction() {
      this.showState = false
      this.fetchData()
    },
    closeAuthorityAction() {
      this.authorityState = false
    },
  },
}
</script>
<style scoped></style>
